package org.opensuse.bs.ui.wizards;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.opensuse.bs.ui.OSC;

public class BSPage extends WizardPage {
	private Text url;
	
	private Combo urls;
	
	private Text user;
	
	private Text password;
	
	private HashMap<String, HashMap<String, String>> sections;
	
	private ModifyListener changedListener = new ModifyListener() {
		public void modifyText(ModifyEvent e) {
			if(e.widget == urls) {
				int index = urls.getSelectionIndex();
				if(index == -1) {
					user.setText("");
					password.setText("");
				}
				else {
					String location = urls.getItem(index);
					HashMap<String, String> section = sections.get(location);
					user.setText(section.get("user"));
					password.setText(section.get("pass"));
				}
			}
			setPageComplete(validatePage());
		}
	};
	
	private boolean validatePage() {
		if(url.isEnabled()) {
			if(url.getText().equals("") || user.getText().equals("") ||
					password.getText().equals("")) {
				return false;
			}
		}
		else {
			if(urls.getSelectionIndex() == -1 || user.getText().equals("") ||
					password.getText().equals("")) {
				return false;
			}
		}
		return true;
	}
	
	public BSPage(HashMap<String, HashMap<String, String>> sections) {
		super("Build Service Login");
		setDescription("You must provide an api url to use Build Service, " +
				"with authentication information. The commonly used api url is " +
				"\"api.opensuse.org\". " +
				"You can either enter a new api url " +
				"or use an existing one.");
		this.sections = sections;
	}

	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout glayout = new GridLayout();
		composite.setLayout(glayout);
		glayout.numColumns = 3;
		
		//choice 1: create a new section
		Button button = new Button(composite, SWT.RADIO);
		button.setText("New API URL: ");
		url = new Text(composite, SWT.BORDER | SWT.SINGLE);
		url.setEnabled(false);
		url.addModifyListener(changedListener);
		GridData gd3 = new GridData();
		url.setLayoutData(gd3);
		gd3.horizontalSpan = 2;
		gd3.grabExcessHorizontalSpace = true;
		gd3.widthHint = 535;
		gd3.heightHint = 18;
		
		//choice 2: select and maybe modify an existing section
		Button button1 = new Button(composite, SWT.RADIO);
		button1.setText("Select Existing URL: ");
		urls = new Combo(composite, SWT.DROP_DOWN);
		Set<String> keys = sections.keySet();
		for(String key : keys) {
			if(!key.equals("general")) {
				urls.add(key);
			}
		}
		GridData gd5 = new GridData();
		urls.setLayoutData(gd5);
		gd5.grabExcessHorizontalSpace = true;
		gd5.widthHint = 545;
		gd5.horizontalSpan = 2;
		urls.setEnabled(false);
		urls.addModifyListener(changedListener);
		
		button.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {}

			public void widgetSelected(SelectionEvent e) {
				url.setEnabled(true);
				urls.setEnabled(false);
				//label modified is nonsence when user want to create new a section
				user.setText("");
				password.setText("");
			}			
		});
		button1.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {}

			public void widgetSelected(SelectionEvent e) {
				urls.setEnabled(true);
				url.setEnabled(false);
				int index = urls.getSelectionIndex();
				if(index != -1) {
					String location = urls.getItem(index);
					HashMap<String, String> section = sections.get(location);
					user.setText(section.get("user"));
					password.setText(section.get("pass"));
				}
			}			
		});
		
		// area for setting user and password
		Group group = new Group(composite, SWT.SHADOW_ETCHED_IN);
		group.setText("Authentication");
		GridData gd4 = new GridData();
		group.setLayoutData(gd4);
		gd4.grabExcessHorizontalSpace = true;
		gd4.horizontalSpan = 3;
		GridLayout glayout1 = new GridLayout();
		group.setLayout(glayout1);
		glayout1.numColumns = 3;
		
		Label label2  = new Label(group, SWT.NULL);
		label2.setText("Username: ");
		
		user = new Text(group, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData();
		user.setLayoutData(gd);
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalSpan = 2;
		gd.widthHint = 600;
		user.addModifyListener(changedListener);
		
		Label label3 = new Label(group, SWT.NULL);
		label3.setText("Password: ");
		
		password = new Text(group, SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
		password.addModifyListener(changedListener);
		GridData gd1 = new GridData();
		password.setLayoutData(gd1);
		gd1.grabExcessHorizontalSpace = true;
		gd1.horizontalSpan = 2;
		gd1.widthHint = 600;
		
		setControl(composite);
		setPageComplete(validatePage());
	}
	
	public String doFinish() {
		String location = "";
		Boolean dirty = false;
		if(url.isEnabled()) {
			location = url.getText().trim();
			dirty = true;
		}
		else {
			int nowUrl = urls.getSelectionIndex();
			if(nowUrl != -1) {
				location = urls.getItem(nowUrl);
				HashMap<String, String> tmp = sections.get(location);
				if(!tmp.get("user").equals(user.getText()) ||
						!tmp.get("pass").equals(password.getText())) {
					dirty = true;
				}
			}
		}
		if(dirty) {
			HashMap<String, String> newSection = new HashMap<String, String>();
			newSection.put("user", user.getText().trim());
			newSection.put("pass", password.getText());
			sections.put(location, newSection);
			try {
				OSC.writeback(sections);
			}catch(IOException e) {
				System.out.println(e.getMessage());
			}
		}
		return location;
	}

}
